package com.example.teacherservice.controller;

import com.example.teacherservice.dto.*;
import com.example.teacherservice.service.CourseDetailService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/course-detail")
public class CourseDetailController {

    private final CourseDetailService courseDetailService;

    public CourseDetailController(CourseDetailService courseDetailService) {
        this.courseDetailService = courseDetailService;
    }

    @GetMapping("/{courseId}")
    public CourseDetailDTO getCourseDetail(
            @PathVariable Long courseId,
            @RequestParam(required = false) Long studentId) {
        return courseDetailService.getCourseDetail(courseId, studentId);
    }

    @GetMapping("/{courseId}/purchase-status")
    public PurchaseStatusDTO checkPurchaseStatus(
            @PathVariable Long courseId,
            @RequestParam Long studentId) {
        return courseDetailService.checkPurchaseStatus(courseId, studentId);
    }

    @PostMapping("/{courseId}/purchase")
    public OrderDTO purchaseCourse(
            @PathVariable Long courseId,
            @RequestBody PurchaseRequestDTO request) {
        return courseDetailService.purchaseCourse(courseId, request.getStudentId());
    }

    @PostMapping("/{courseId}/review")
    public ResponseEntity<CourseAppraiseDTO> addCourseReview(
            @PathVariable Long courseId,
            @RequestBody ReviewRequestDTO request) {

        if (request.getUserId() == null) {
            return ResponseEntity.badRequest().build();
        }

        CourseAppraiseDTO review = courseDetailService.addCourseReview(
                courseId,
                request.getUserId(),  // 从请求体获取
                request.getScore(),
                request.getContent()
        );

        return ResponseEntity.ok(review);
    }
}